package service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import common.dao.BookMapper;
import common.dao.OrderMapper;
import common.pojo.Book;
import common.pojo.Count;
import common.pojo.Order;
import common.utils.Constants;

@Service
public class OrderServiceImpl {

	@Autowired
	private OrderMapper mapper;

	@Autowired
	private BookMapper bmapper;

	/**
	 * 查询数量
	 * 
	 * @param bType
	 * @return
	 */
	public int queryByCount(Order order) {
		return mapper.queryByCount(order);
	}

	/**
	 * 查询列表
	 * 
	 * @param start
	 * @param limit
	 * @param state
	 * @return
	 */
	public List<Order> find(int start, int limit, String state) {
		String where = " o join member m on o.member_id=m.id where o.deleted="
				+ Constants.NOT_DELETED;
		if (StringUtils.isNotEmpty(state)) {
			where += " and o.state=" + state;
		}
		return mapper.queryListByWhere(where
				+ " order by o.create_time desc limit " + start + "," + limit);
	}

	/**
	 * 查詢
	 * 
	 * @param start
	 * @param limit
	 * @param state
	 * @param key
	 * @return
	 */
	public List<Order> search(int start, int limit, String state,
			String orderdate, String key) {
		String where = " o join member m on o.member_id=m.id where o.deleted="
				+ Constants.NOT_DELETED;
		if (StringUtils.isNotEmpty(state)) {
			where += " and o.state=" + state;
		}
		if (StringUtils.isNotEmpty(key)) {
			where += " and o.id=" + key;
		}
		if (StringUtils.isNotEmpty(orderdate)) {
			where += " and o.order_time like '%" + orderdate + "%' ";
		}
		return mapper.queryListByWhere(where
				+ " order by o.create_time desc limit " + start + "," + limit);
	}

	public int queryByCount(String state, String orderdate, String key) {
		String where = " o join member m on o.member_id=m.id where o.deleted="
				+ Constants.NOT_DELETED;
		if (StringUtils.isNotEmpty(state)) {
			where += " and o.state=" + state;
		}
		if (StringUtils.isNotEmpty(key)) {
			where += " and o.id=" + key;
		}
		if (StringUtils.isNotEmpty(orderdate)) {
			where += " and o.order_time like '%" + orderdate + "%' ";
		}
		return mapper.queryCountByWhere(where);
	}

	public void updateBySelective(Order order) {
		mapper.updateBySelective(order);
	}

	public Order findInfoById(String id) {
		// TODO Auto-generated method stub
		return mapper.queryById(Integer.parseInt(id));
	}

	

}
